package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.MemberMapper;
import com.mapper.OrderMapper;
import com.pageResult.PageResult;
import com.pojo.Member;
import com.pojo.MemberExample;
import com.pojo.Order;
import com.pojo.OrderExample;
import com.query.QueryPageBean;
import com.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public List<Integer> getMemberCount(List<String> months) {
        List<Integer> count=new ArrayList<>();
        if(months!=null && months.size()>0){
            months.forEach(m->{
                Integer memberCount = memberMapper.getMemberCount(m + ".01");
                count.add(memberCount);
            });
        }
        return count;
    }

    @Override
    public PageResult selectByQueryString(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<Member> page = memberMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Member member) {
        member.setRegtime(new Date());
        memberMapper.insertSelective(member);
    }

    @Override
    public void update(Member member) {
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public Member queryById(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void delete(Integer id) throws CustomException {
        OrderExample oe=new OrderExample();
        OrderExample.Criteria oec = oe.createCriteria();
        oec.andMemberIdEqualTo(id);
        List<Order> orders = orderMapper.selectByExample(oe);
        if(orders!=null && orders.size()>0){
            throw new CustomException(MessageConstant.MEMBER_IN_USE);
        }
        memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Member login(String phoneNumber) {
        MemberExample me=new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andPhonenumberEqualTo(phoneNumber);
        List<Member> memberList = memberMapper.selectByExample(me);
        //1.通过手机号码判断是不是会员
        if(memberList!=null && memberList.size()>0){
            return memberList.get(0);
        }
        //不是会员 员则自动完成会员注册
        Member member=new Member();
        member.setRegtime(new Date());
        member.setPhonenumber(phoneNumber);
        memberMapper.insertSelective(member);
        return member;
    }
}
